<template>
  <div class="table-page">
    <GiTable row-key="id" title="商户列表管理" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #custom-left>
        <a-input v-model="queryForm.accountName" placeholder="请输入账户名" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.nickName" placeholder="请输入昵称" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['generator:account:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
        <a-tooltip content="导出">
          <a-button v-permission="['generator:account:export']" class="gi_hover_btn-border" @click="onExport">
            <template #icon>
              <icon-download />
            </template>
          </a-button>
        </a-tooltip>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #headImg="{ record }">
        <GiCellAvatar v-if="record.headImg" :avatar="record.headImg" />
      </template>
      <template #isActive="{ record }">
        <a-tag v-if="record.isActive" color="arcoblue" size="small">是</a-tag>
        <a-tag v-else color="red" size="small">否</a-tag>
      </template>
      <template #isShop="{ record }">
        <a-tag v-if="record.isShop" color="arcoblue" size="small">是</a-tag>
        <a-tag v-else color="red" size="small">否</a-tag>
      </template>
      <template #shopStatus="{ record }">
        <a-tag v-if="record.shopStatus" color="arcoblue" size="small">是</a-tag>
        <a-tag v-else color="red" size="small">否</a-tag>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['generator:account:update']" @click="onUpdate(record)">修改</a-link>
          <!-- <a-link v-permission="['generator:account:delete']" status="danger" :disabled="record.disabled"
            @click="onDelete(record)">
            删除
          </a-link> -->
          <a-link v-if="!record.isFreeze" status="warning" @click="onFreeze(record)">
            冻结
          </a-link>
          <a-link v-else status="success" @click="onUnFreeze(record)">
            解冻
          </a-link>
          <a-dropdown>
            <a-link v-if="has.hasPermOr(['system:user:resetPwd'])" type="text">更多</a-link>
            <template #content>
              <a-doption @click="onResetPwd(record)">重置密码</a-doption>
            </template>
          </a-dropdown>
</a-space>
      </template>
    </GiTable>

    <AccountAddModal ref="AccountAddModalRef" @save-success="search" />
    <AccountDetailDrawer ref="AccountDetailDrawerRef" />
    <AccountResetPwdModal ref="AccountResetPwdModalRef" />
  </div>
</template>

<script setup lang="ts">
import AccountAddModal from './AccountAddModal.vue'
import AccountDetailDrawer from './AccountDetailDrawer.vue'
import AccountResetPwdModal from './AccountResetPwdModal.vue'
import { type AccountQuery, type AccountResp, deleteAccount, exportAccount, freezeAccount, listAccount, unFreezeAccount } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'Account' })

const queryForm = reactive<AccountQuery>({
  accountName: '',
  name: '',
  nickName: '',
  sort: ['createTime,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listAccount({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '账户名', dataIndex: 'accountName', width: 150, slotName: 'accountName' },
  { title: '昵称', dataIndex: 'nickName', slotName: 'nickName' },
  { title: '头像', dataIndex: 'headImg', slotName: 'headImg' },
  // { title: '是否会员', dataIndex: 'isActive', slotName: 'isActive' },
  // { title: '是否商户', dataIndex: 'isShop', slotName: 'isShop' },
  { title: '店铺认证', dataIndex: 'shopStatus', slotName: 'shopStatus' },
  { title: '店铺码', dataIndex: 'shopCode', slotName: 'shopCode' },
  { title: '余额', dataIndex: 'shopAmount', slotName: 'shopAmount' },
  { title: '积分', dataIndex: 'scoreAmount', slotName: 'scoreAmount' },
  { title: '邀请人', dataIndex: 'parentAccountName', slotName: 'parentAccountName', width: 150 },
  // { title: '邀请码', dataIndex: 'invitationCode', slotName: 'invitationCode' },
  { title: '注册时间', dataIndex: 'createTime', slotName: 'createTime', width: 180 },
  {
    title: '操作',
    slotName: 'action',
    width: 200,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['generator:account:update', 'generator:account:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.accountName = ''
  queryForm.name = ''
  queryForm.nickName = ''
  search()
}

// 删除
const onDelete = (item: AccountResp) => {
  return handleDelete(() => deleteAccount(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 冻结
const onFreeze = (item: AccountResp) => {
  return handleDelete(() => freezeAccount(item.id), {
    content: `是否确定冻结该商户？`,
    showModal: true
  })
}

// 解冻
const onUnFreeze = (item: AccountResp) => {
  return handleDelete(() => unFreezeAccount(item.id), {
    content: `是否确定解冻该商户？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportAccount(queryForm))
}

const AccountAddModalRef = ref<InstanceType<typeof AccountAddModal>>()
// 新增
const onAdd = () => {
  AccountAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: AccountResp) => {
  AccountAddModalRef.value?.onUpdate(item.id)
}

const AccountDetailDrawerRef = ref<InstanceType<typeof AccountDetailDrawer>>()
// 详情
const onDetail = (item: AccountResp) => {
  AccountDetailDrawerRef.value?.onDetail(item.id)
}

const AccountResetPwdModalRef = ref<InstanceType<typeof AccountResetPwdModal>>()
// 重置密码
const onResetPwd = (item: AccountResp) => {
  AccountResetPwdModalRef.value?.onReset(item.id)
}
</script>

<style lang="scss" scoped></style>
